<div id="userlist">
<br/>
<center>
<hr style="color:#e6af2b"/>
<table>
	<tr>
		<th width="300">Student Name</th>
<?php foreach($grades as $exam) : ?>
		<th><?php echo $exam['name']; ?></th>
		<?php $exam_ids[] = $exam['exam_id'] ?>
<?php endforeach; ?>
		<th><strong>Average</strong></th>
	</tr>
<?php
foreach( $grades as $user ) {
	$userlookup[ $user['stud_no'] ] = $user;
}
foreach( $grades as $grade ) {
	$gradelookup[ $grade['stud_no'] ][ $grade['exam_id'] ] = sprintf('%.2f', ($grade['num_correct']/$grade['items']) * 100 );
}
?>

<?php 	foreach( $userlookup as $user ) :  ?>
<?php if( $user['exam_session_id'] === null ) continue ?>
	<tr style="font-size: 11px; font-family: verdana;">
		<td>
			<?php echo ucwords( strtolower( $user['lastname'] )) ?>
			<?php echo ucwords( strtolower( $user['firstname'] )) ?>
			<?php echo ucwords( strtolower( $user['middlename'][0] )) . '.' ?>
		</td>
<?php $total = 0 ?>
<?php foreach( $exam_ids as $exam ) : ?>
		<?php $total += $gradelookup[ $user['stud_no'] ][ $exam ] ?>
		<td><?php echo $gradelookup[ $user['stud_no'] ][ $exam ] != '' ? $gradelookup[ $user['stud_no'] ][ $exam ].'%' : '-'  ?></td>
<?php endforeach; ?>
		<td><?php printf('%.2f', $total/count($exam_ids) ) ?>%</td>
	</tr>
<?php 	endforeach;?>
</table>
</center>

</div>
